package cn.itcast.hotel.mq;

import cn.itcast.hotel.constatnts.MqConstants;
import cn.itcast.hotel.service.IHotelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class HotelListener {

    @Autowired
    private IHotelService hotelService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 监听酒店新增或修改的业务
     * @param id 酒店id
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(MqConstants.HOTEL_INSERT_QUEUE),
            exchange = @Exchange(value = MqConstants.HOTEL_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.HOTEL_INSERT_KEY))
    public void listenHotelInsertOrUpdate(Long id){
        hotelService.insertById(id);
        log.info("新增数据成功 {}",id);
    }

    /**
     * 监听酒店删除的业务
     * @param id 酒店id
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(MqConstants.HOTEL_DELETE_QUEUE),
            exchange = @Exchange(value = MqConstants.HOTEL_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.HOTEL_DELETE_KEY))
    public void listenHotelDelete(Long id){
        hotelService.deleteById(id);
        log.info("删除数据成功 {}",id);
    }
}
